package us.icitap.timsportal.ejb;

import java.util.Hashtable;

import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

public class DomainTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Hashtable<String, String> env;
		DirContext ctx;
		try {
			env = new Hashtable<String, String>();
			env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
			env.put(Context.PROVIDER_URL, "LDAP://192.168.150.10:389/DC=ASP,DC=gov,DC=al");
			env.put(Context.SECURITY_AUTHENTICATION, "simple");
			env.put(Context.SECURITY_PRINCIPAL, "bruno.mertiku" + "@asp.gov.al");
			env.put(Context.SECURITY_CREDENTIALS, "policia2012"); // the password
			env.put(Context.REFERRAL, "ignore");
			env.put("com.sun.jndi.ldap.read.timeout", "5000");
			ctx = new InitialDirContext(env);
			
			// Specify the ids of the attributes to return
			String[] attrIDs = {"sAMAccountName", "userPrincipalName", "distinguishedName"};
			SearchControls ctls = new SearchControls();
			ctls.setReturningAttributes(attrIDs);
			ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
			ctls.setTimeLimit(5000);

			// Specify the search filter to match
			// Ask for objects that have the attribute "sn" == "Geisel"
			// and the "mail" attribute
			String filter = "(&(sn=Bushati)(mail=*))";

			// Search the subtree for objects by using the filter
			NamingEnumeration<SearchResult> answer = ctx.search("", filter, ctls);
						
			while (answer.hasMoreElements()) {
				System.out.println(">>>"+ answer.hasMoreElements());
			    SearchResult sr = (SearchResult)answer.nextElement();
			    System.out.println(">>>" + sr.getName());

			    Attributes ans = ctx.getAttributes(sr.getName());
			    
//			    for (NamingEnumeration ae = ans.getAll(); ae.hasMore();) {
//			        Attribute attr = (Attribute)ae.next();
//			        System.out.println("attribute: " + attr.getID());
//
//			        for (NamingEnumeration e = attr.getAll(); e.hasMore();
//			             System.out.println("value: " + e.next()))
//			            ;
//			    }
			}
			
			System.out.println("DomainTest.main()");
			ctx.close();
		} catch (AuthenticationException e){	
		} catch (NamingException ne) {
			System.err.println("DirContext fail !");
			ne.printStackTrace();
		} finally {
			
		}
		
		System.out.println("DomainTest.enclosing_method()");

	}

}
